from fastapi import UploadFile, File

from doc.parser_doc import DocParser
from doc.parser_pdf import PdfParser
from doc.parser_xls import XlsParser
from server import app, error, success


@app.post("/ocr/pdf")
async def ocr_pdf(mode: str, file: UploadFile = File(...)):
    parser = PdfParser()
    if not parser.can_parse(file.filename):
        return error(400, message="Invalid File")
    if mode is None or mode == '':
        return error(400, message="Invalid Mode")
    result = await parser.parse_upload_file_to_text(file, mode)
    return success(result)


@app.post("/parse/doc")
async def parse_doc(file: UploadFile = File(...)):
    parser = DocParser()
    if not parser.can_parse(file.filename):
        return error(400, message="Invalid File")
    result = await parser.parse_upload_file_to_text(file)
    return success(result)


@app.post("/parse/xls")
async def parse_xls(file: UploadFile = File(...)):
    parser = XlsParser()
    if not parser.can_parse(file.filename):
        return error(400, message="Invalid File")
    result = await parser.parse_upload_file_to_text(file)
    return success(result)

